/*
 * @Descripttion:
 * @version:
 * @Author: kl2018
 * @Date: 2020-03-17 09:11:32
 * @LastEditors: kl2018
 * @LastEditTime: 2020-10-29 10:54:04
 */

import { observable, action, toJS } from 'mobx';
import { WeaForm } from 'comsMobx';
import * as Apis from './../../apis/commom/tabConpenentApi';


class TabComponentStore {
  @observable showSearchAd = false;// 高级搜索的显隐
  @observable searchForm = new WeaForm(); // 搜索框的form
  @observable tabData = [];// 页签
  @observable selectedKey = '0';// 选中的页签
  @observable conditions = [];
  @observable items = [];
  @observable topTabCount = {};
  @observable searchValue = '';
  @observable fastForm = new WeaForm();

  // 高级搜索框的显隐
  @action.bound setShowSearchAd (bool) {
    this.showSearchAd = bool;
  }

  // 获取页签数据
  @action.bound
  async getTabData (url, params) {
    try {
      const data = await Apis.getTabData(url, params);
      this.tabData = data.topTab;
      this.topTabCount = data.topTabCount;
    } catch (e) {
      console.log(e);
    }
  }

  // 获取高级搜索的form表单
  @action.bound
  async getSearchFormItem (url, params, baseSearchName, isCustomRender) {
    const resFormParams = await Apis.getSearchFormItem(url, params).then((data) => {
      this.searchForm = new WeaForm();
      this.fastForm = new WeaForm();
      this.searchForm.reset();
      this.fastForm.reset();
      const conditionInfo = data.conditionInfo || data.condition || data.conditions;
      this.conditions = conditionInfo;
      this.items = (data.showSearchcondition || conditionInfo)[0].items;
      this.fastForm.setCondition((data.showSearchcondition || conditionInfo));
      if (conditionInfo) {
        this.searchForm.setCondition(conditionInfo);
      } else if (data.items) {
        this.searchForm.setCondition(data.items);
      }
      if (isCustomRender) {
        this.searchForm.initFormFields(toJS(conditionInfo));
      }
      const formParams = this.searchForm.getFormParams();
      const fastSearchFormParams = this.fastForm.getFormParams();
      if (baseSearchName && formParams[baseSearchName]) {
        this.searchValue = formParams[baseSearchName];
      }
      return {
        ...formParams,
        ...fastSearchFormParams
      };
    });
    return resFormParams;
  }

  // 初始化数据
  @action.bound initData () {
    this.tabData = [];
    this.searchForm = new WeaForm();
    this.selectedKey = '0';
    this.topTabCount = [];
    this.conditions = [];
    this.showSearchAd = false;
    this.searchValue = '';
  }

  @action.bound
  setMobxState (params = {}) {
    Object.keys(params).map((key) => {
      this[key] = params[key];
    });
  }
}
const tabComponentStore = new TabComponentStore();
export default tabComponentStore;
